package rectangulo;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;

public class Main extends JFrame implements MouseListener {
	private int height;
	private int width;
	private int ancho;
	private int alto;;
	private int x;
	private int y;
	private Rect p;
	private int rAncho;
	private int rAlto;

	public Main() {
		ventana();
	}

	private void ventana() {
		this.alto = 500;
		this.ancho = 500;
		this.height = Toolkit.getDefaultToolkit().getScreenSize().height;
		this.width = Toolkit.getDefaultToolkit().getScreenSize().width;
		int x = width / 2 - ancho / 2;
		int y = height / 2 - alto / 2;
		setTitle("Ventana");
		setSize(ancho, alto);
		setLocation(x, y);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		colocarPanel();
		addMouseListener(this);
		setVisible(true);
	}

	private void colocarPanel() {
		this.x = (new Random()).nextInt(500);
		this.y = (new Random()).nextInt(500);
		this.rAncho = 50 + (new Random()).nextInt(51);
		this.rAlto = 50 + (new Random()).nextInt(51);

		p = new Rect(x, y, rAncho, rAlto);

		add(p);
		// paint(getGraphics());
		p.repaint();

	}

	public static void main(String[] args) {
		new Main();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		int clicX = e.getX() - 9;
		int clicY = e.getY() - 30;
		System.out.println("(" + clicX + "," + clicY + ")");
		System.out.println("[" + (x) + "," + (y) + ")");
		if (clicX < x + rAncho && clicX > x && clicY > y && clicY < y + rAlto) {
			System.out.println("Pinchado");
			p.setRelleno(true);
			p.repaint();
		} else {
			p.setRelleno(false);
			p.repaint();
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
}
